Public Class ArtistMaintenance

    Public Event ArtistsUpdated()

    Private _newArtists As New System.Collections.ObjectModel.Collection(Of String)
    Private _deletedArtists As New System.Collections.ObjectModel.Collection(Of String)

    Private Sub ArtistMaintenance_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.VendorTableAdapter.Fill(Me.VendorData.Vendor)

    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click

        ArtistDS.CancelEdit()
        Me.Close()

    End Sub

    Private Sub UpdateArtists_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateArtists.Click

        Const ExpectedMessage As String = "The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship.  Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again."

        Dim updated As Integer

        For Each deletedProvider As String In _deletedArtists
            Me.VendorTableAdapter.Delete(deletedProvider)
        Next

        ArtistDS.EndEdit()

        Try

            updated = VendorTableAdapter.Update(VendorData.Vendor)

        Catch ex As OleDb.OleDbException
            If ex.Message <> ExpectedMessage Then
                MsgBox(ex.Message)
            End If
        End Try

        RaiseEvent ArtistsUpdated()

        Me.Close()

    End Sub

    Private Sub AddArtist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddArtist.Click

        If VendorName.Text.Trim.Length > 0 Then
            Dim newProvider As VendorData.VendorRow = VendorData.Vendor.NewVendorRow
            newProvider.vendor_name = VendorName.Text.Trim
            Try
                VendorData.Vendor.AddVendorRow(newProvider)
                _newArtists.Add(VendorName.Text.Trim)
            Catch ex As Exception

            End Try
            VendorList.Refresh()
        End If

    End Sub

    Private Sub RemoveArtist_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RemoveArtist.Click

        Dim entryRow As VendorData.VendorRow
        Dim rowsToDelete As New System.Collections.ObjectModel.Collection(Of VendorData.VendorRow)

        For Each entry As System.Data.DataRowView In VendorList.SelectedItems
            entryRow = CType(entry.Row, VendorData.VendorRow)
            If entryRow.RowState = DataRowState.Added Then
                _newArtists.Remove(entryRow.vendor_name)
            Else
                _deletedArtists.Add(entryRow.vendor_name)
            End If
            rowsToDelete.Add(entryRow)
        Next

        For Each entryRow In rowsToDelete
            VendorData.Vendor.RemoveVendorRow(entryRow)
        Next

        VendorList.Refresh()

    End Sub

End Class